home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / ras8to1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  16.5 KB  |  628 lines

  1. /*
  2.  * Modified by Phill Everson <everson@cs.bris.ac.uk> for inclusion into
  3.  * the ALV toolkit.
  4.  *
  5.  */
  6.  
  7. /**************************************************************************
  8.  Copyright (c) 1988 by Hong Min
  9.  
  10.    compile : cc -O -o 8to1 8to1.c -lpixrect
  11.  
  12.    The program 8to1 converts a 8-bit depth sun raster file (both color,
  13.    i.e. rgb are different, and grey, i.e. rgb are the same, and both
  14.    standard format and byte-encoded format) to a 1-bit depth mono sun
  15.    raster file. If your color image has no colormap, this program would
  16.    provide a default grey scale color map. It implements several halftone
  17.    algorithms from Digital Halftones by Dot Diffusion in acm Transactions
  18.    on Graphics, such as error diffusion, ordered dither and dot diffusion
  19.    with and without edge enhancement.  Just type 8to1, it will give you
  20.    the usage.
  21.  
  22.    This program was written by :
  23.  
  24.     Hong Min
  25.     Computer Science Department
  26.        SUNY at Stony Brook
  27.     e-mail address :
  28.         UUCP: {allegra, philabs, pyramid, research}!sbcs!rainbow
  29.         ARPA-Internet: rainbow@sbcs.sunysb.edu
  30.         CSnet: rainbow@suny-sb
  31.  
  32.    Everyone is welcome to write to me if you like the program or not.
  33.    And welcome to optimize the code, as I haven't fooled with it to
  34.    make it efficient yet.
  35.  
  36.         Enjoy!
  37.  
  38. ****************************************************************************/
  39. #include <stdio.h>
  40. #include "defs.h"
  41. #include <sys/types.h>
  42. #include <pixrect/pixrect.h>
  43. #include <pixrect/memvar.h>
  44. #include <pixrect/pr_util.h>
  45. #include <pixrect/pr_io.h>
  46.  
  47. #define WHITE   0        /* background color */
  48. #define BLACK   ~0        /* foreground color */
  49. #define FALSE    0
  50. #define TRUE    1
  51. #define ALPHA    7
  52. #define BETA    3
  53. #define GAMMA    5
  54. #define DELTA    1
  55.  
  56. hort **A, **B;
  57. nt ordered_dither[8][8] = {0, 32, 8, 40, 2, 34, 10, 42,
  58.     48, 16, 56, 24, 50, 18, 58, 26,
  59.     12, 44, 4, 36, 14, 46, 6, 38,
  60.     60, 28, 52, 20, 62, 30, 54, 22,
  61.     3, 35, 11, 43, 1, 33, 9, 41,
  62.     51, 19, 59, 27, 49, 17, 57, 25,
  63.     15, 47, 7, 39, 13, 45, 5, 37,
  64. 63, 31, 55, 23, 61, 29, 53, 21};
  65. nt dot_diffusion[8][8] = {34, 48, 40, 32, 29, 15, 23, 31,
  66.     42, 58, 56, 53, 21, 5, 7, 10,
  67.     50, 62, 61, 45, 13, 1, 2, 18,
  68.     38, 46, 54, 37, 25, 17, 9, 26,
  69.     28, 14, 22, 30, 35, 49, 41, 33,
  70.     20, 4, 6, 11, 43, 59, 57, 52,
  71.     12, 0, 3, 19, 51, 63, 60, 44,
  72. 24, 16, 8, 27, 39, 47, 55, 36};
  73. nt reverse_matrix[64] = {49, 21, 22, 50, 41, 13, 42, 14,
  74.     58, 30, 15, 43, 48, 20, 33, 5,
  75.     57, 29, 23, 51, 40, 12, 34, 6,
  76.     56, 28, 31, 59, 32, 4, 35, 7,
  77.     3, 39, 0, 36, 63, 27, 24, 60,
  78.     2, 38, 8, 44, 55, 19, 25, 61,
  79.     1, 37, 16, 52, 47, 11, 26, 62,
  80. 10, 46, 9, 45, 54, 18, 17, 53};
  81.  
  82. har *progname;
  83. har *filename;
  84. ixrect *pr = 0;
  85.  
  86. #define ERROR    0
  87. #define ORDERED    1
  88. #define DOT    2
  89.  
  90. #ifdef STANDALONE
  91. ain(argc, argv, envp)
  92. #else
  93. as8to1_main(argc, argv, envp)
  94. #endif
  95.     int argc;
  96.     char **argv;
  97.     char **envp;
  98. {
  99.     int diffusion, enhancement, colour, greyscale, option;
  100.     int default_map = FALSE;
  101.     register int i, j;
  102.     int levels;
  103.     colormap_t colormap;
  104.     register unsigned char *map;
  105.     register struct rasterfile rh;
  106.  
  107.     colour = TRUE;
  108.     diffusion = ERROR;
  109.     enhancement = FALSE;
  110.     progname = strsave(argv[0]);
  111.     parse_profile(&argc, argv, envp);
  112.  
  113.     while ((gc = getopt(argc, argv, "rodeg")) != EOF)
  114.         switch (gc) {
  115.         case 'r':
  116.             diffusion = ERROR;
  117.             break;
  118.         case 'o':
  119.             diffusion = ORDERED;
  120.             break;
  121.         case 'd':
  122.             diffusion = DOT;
  123.             break;
  124.         case 'e':
  125.             enhancement = TRUE;
  126.             break;
  127.         case 'g':
  128.             colour = FALSE;
  129.             break;
  130.         case '?':
  131.             errflag++;
  132.             break;
  133.         }
  134.  
  135.     if (errflag)
  136.         error((char *) 0, "Usage: %s: [-r] [-o] [-d] [-e] [-g] [infile] [outfile]", progname);
  137.  
  138.     for (stream = 0; optind < argc; stream++, optind++)
  139.         if (stream < 2 && strcmp(argv[optind], "-") != 0)
  140.             if (freopen(argv[optind], mode[stream], f[stream]) == NULL)
  141.                 error("%s %s", PR_IO_ERR_INFILE, argv[optind]);
  142.  
  143.     setup_greylevelmap();
  144.  
  145.     /* Load the input rasterfile header */
  146.     if (pr_load_header(stdin, &rh) == PIX_ERR)
  147.         error(PR_IO_ERR_RASREAD);
  148.  
  149.     if (rh.ras_depth != 8)
  150.         error("infile is not a 8 bits deep");
  151.  
  152.     /* Load the colormap */
  153.     colormap.type = RMT_NONE;
  154.     colormap.map[0] = (unsigned char *) malloc(256);
  155.     colormap.map[1] = (unsigned char *) malloc(256);
  156.     colormap.map[2] = (unsigned char *) malloc(256);
  157.     if (pr_load_colormap(stdin, &rh, &colormap) == PIX_ERR)
  158.         error("read colormap error");
  159.  
  160.     if (colormap.type != RMT_NONE &&
  161.         (colormap.type != RMT_EQUAL_RGB || colormap.length < 256))
  162.         error("input has unsupported colormap type or length\n");
  163.  
  164.     if ((colormap.type == RMT_NONE) && (colormap.length == 0)) {
  165.         default_map = TRUE;
  166.         map = greylevelmap;
  167.         colour = FALSE;
  168.     }
  169.     if (rh.ras_type != RT_OLD && rh.ras_type != RT_STANDARD &&
  170.         !(pr = pr_load_image(stdin, &rh, &colormap))) {
  171.         fprintf(stderr, "error reading rasterfile\n");
  172.         exit(1);
  173.     }
  174.     option = diffusion;
  175.     if (enhancement)
  176.         option += 3;
  177.     if (colour)
  178.         option += 6;
  179.  
  180.     /* Write new header */
  181.     rh.ras_type = RT_STANDARD;
  182.     rh.ras_depth = 1;
  183.     rh.ras_length = mpr_linebytes(rh.ras_width, 1) * rh.ras_height;
  184.     rh.ras_maptype = RMT_NONE;
  185.     rh.ras_maplength = 0;
  186.  
  187.     if (pr_dump_header(stdout, &rh, (colormap_t *) 0) == PIX_ERR)
  188.         error("pr_dump_header failed");
  189.  
  190.     if (rh.ras_width <= 0 || rh.ras_height <= 0)
  191.         exit(1);
  192.     else {
  193.         A = (short **) malloc((rh.ras_width + 3) * sizeof(short *));
  194.         B = (short **) malloc((rh.ras_width + 3) * sizeof(short *));
  195.         for (i = 0; i < rh.ras_width + 3; i++) {
  196.             A[i] = (short *) malloc((rh.ras_height + 3) * sizeof(short));
  197.             B[i] = (short *) malloc((rh.ras_height + 3) * sizeof(short));
  198.         }
  199.     }
  200.  
  201.     if (!default_map)
  202.         map = colormap.map[0];
  203.  
  204.     if (pr)
  205.         switch (option) {
  206.         case 6:
  207.             map = (unsigned char *) malloc(256);
  208.             color_map_3_to_1(colormap, map);
  209.         case 0:
  210.             error_diffusion_image(rh.ras_width, rh.ras_height,
  211.                 map, (u_char *) mpr_d(pr)->md_image, stdout,
  212.                  0, mpr_d(pr)->md_linebytes - rh.ras_width);
  213.             break;
  214.         case 7:
  215.             map = (unsigned char *) malloc(256);
  216.             color_map_3_to_1(colormap, map);
  217.         case 1:
  218.             ordered_dither_image(rh.ras_width, rh.ras_height,
  219.                 map, (u_char *) mpr_d(pr)->md_image, stdout,
  220.                  0, mpr_d(pr)->md_linebytes - rh.ras_width);
  221.             break;
  222.         case 8:
  223.             map = (unsigned char *) malloc(256);
  224.             color_map_3_to_1(colormap, map);
  225.         case 2:
  226.             dot_diffusion_image(rh.ras_width, rh.ras_height,
  227.                 map, (u_char *) mpr_d(pr)->md_image, stdout,
  228.                  0, mpr_d(pr)->md_linebytes - rh.ras_width);
  229.             break;
  230.         case 9:
  231.             map = (unsigned char *) malloc(256);
  232.             color_map_3_to_1(colormap, map);
  233.         case 3:
  234.             error_diffusion_image(rh.ras_width, rh.ras_height,
  235.                 map, (u_char *) mpr_d(pr)->md_image, stdout,
  236.                  1, mpr_d(pr)->md_linebytes - rh.ras_width);
  237.             break;
  238.         case 10:
  239.             map = (unsigned char *) malloc(256);
  240.             color_map_3_to_1(colormap, map);
  241.         case 4:
  242.             ordered_dither_image(rh.ras_width, rh.ras_height,
  243.                 map, (u_char *) mpr_d(pr)->md_image, stdout,
  244.                  1, mpr_d(pr)->md_linebytes - rh.ras_width);
  245.             break;
  246.         case 11:
  247.             map = (unsigned char *) malloc(256);
  248.             color_map_3_to_1(colormap, map);
  249.         case 5:
  250.             dot_diffusion_image(rh.ras_width, rh.ras_height,
  251.                 map, (u_char *) mpr_d(pr)->md_image, stdout,
  252.                  1, mpr_d(pr)->md_linebytes - rh.ras_width);
  253.             break;
  254.         }
  255.     else
  256.         switch (option) {
  257.         case 6:
  258.             map = (unsigned char *) malloc(256);
  259.             color_map_3_to_1(colormap, map);
  260.         case 0:
  261.             error_diffusion_file(rh.ras_width, rh.ras_height,
  262.                          map, stdin, stdout, 0,
  263.                  mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  264.             break;
  265.         case 7:
  266.             map = (unsigned char *) malloc(256);
  267.             color_map_3_to_1(colormap, map);
  268.         case 1:
  269.             ordered_dither_file(rh.ras_width, rh.ras_height,
  270.                         map, stdin, stdout, 0,
  271.                  mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  272.             break;
  273.         case 8:
  274.             map = (unsigned char *) malloc(256);
  275.             color_map_3_to_1(colormap, map);
  276.         case 2:
  277.             dot_diffusion_file(rh.ras_width, rh.ras_height,
  278.                        map, stdin, stdout, 0,
  279.                  mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  280.             break;
  281.         case 9:
  282.             map = (unsigned char *) malloc(256);
  283.             color_map_3_to_1(colormap, map);
  284.         case 3:
  285.             error_diffusion_file(rh.ras_width, rh.ras_height,
  286.                          map, stdin, stdout, 1,
  287.                  mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  288.             break;
  289.         case 10:
  290.             map = (unsigned char *) malloc(256);
  291.             color_map_3_to_1(colormap, map);
  292.         case 4:
  293.             ordered_dither_file(rh.ras_width, rh.ras_height,
  294.                         map, stdin, stdout, 1,
  295.                  mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  296.             break;
  297.         case 11:
  298.             map = (unsigned char *) malloc(256);
  299.             color_map_3_to_1(colormap, map);
  300.         case 5:
  301.             dot_diffusion_file(rh.ras_width, rh.ras_height,
  302.                        map, stdin, stdout, 1,
  303.                  mpr_linebytes(rh.ras_width, 8) - rh.ras_width);
  304.             break;
  305.         }
  306. }
  307.  
  308. olor_map_3_to_1(colormap, map)
  309.     register colormap_t colormap;
  310.     register unsigned char *map;
  311. {
  312.     register unsigned long tmp, i;
  313.  
  314.     for (i = 0; i < 256; i++) {
  315.         tmp = colormap.map[0][i] * 77 + colormap.map[1][i] * 151 + colormap.map[2][i] * 28;
  316.         map[i] = tmp >> 8;
  317.     }
  318. }
  319.  
  320.  
  321. dge_enhancement(width, height, off)
  322.     register int width, height, off;
  323. {
  324.     register int i, j, x, y;
  325.  
  326.     B[off][off] = A[off][off] * 6 - A[off][off + 1] - A[off + 1][off] - A[off + 1][off + 1];
  327.     B[off][off + height - 1] = A[off][off + height - 1] * 6 - A[off][off + height - 2] - A[off + 1][off + height - 1] - A[off + 1][off + height - 2];
  328.     B[off + width - 1][off] = A[off + width - 1][off] * 6 - A[off + width - 1][off + 1] - A[off + width - 2][off] - A[off + width - 2][off + 1];
  329.     B[off + width - 1][off + height - 1] = A[off + width - 1][off + height - 1] * 6 - A[off + width - 1][off + height - 2] - A[off + width - 2][off + height - 1] - A[off + width - 2][off + height - 2];
  330.  
  331.     for (i = 1; i < width - 1; i++)
  332.         B[off + i][off + 0] = A[off + i][off + 0] * 6 - A[off + i - 1][off + 0] - A[off + i + 1][off + 0] - A[off + i - 1][off + 1] - A[off + i][off + 1] - A[off + i + 1][off + 1];
  333.     for (i = 1; i < width - 1; i++)
  334.         B[off + i][off + height - 1] = A[off + i][off + height - 1] * 6 - A[off + i - 1][off + height - 1] - A[off + i + 1][off + height - 2] - A[off + i - 1][off + height - 2] - A[off + i][off + 1] - A[off + i + 1][off + height - 2];
  335.     for (i = 1; i < height - 1; i++)
  336.         B[off + 0][off + i] = A[off + 0][off + i] * 6 - A[off + 0][off + i - 1] - A[off + 0][off + i + 1] - A[off + 1][off + i - 1] - A[off + 1][off + i] - A[off + 1][off + i + 1];
  337.     for (i = 1; i < height - 1; i++)
  338.         B[off + width - 1][off + i] = A[off + width - 1][off + i] * 6 - A[off + width - 1][off + i - 1] - A[off + width - 1][off + i + 1] - A[off + width - 2][off + i - 1] - A[off + width - 2][off + i] - A[off + width - 2][off + i + 1];
  339.  
  340.     for (i = 1; i < width - 1; i++)
  341.         for (j = 1; j < height - 1; j++) {
  342.             B[off + i][off + j] = A[off + i][off + j] * 10;
  343.             for (x = i - 1; x <= i + 1; x++)
  344.                 for (y = j - 1; y <= j + 1; y++)
  345.                     B[off + i][off + j] -= A[off + x][off + y];
  346.         }
  347.  
  348.     for (i = 0; i < width; i++)
  349.         for (j = 0; j < height; j++)
  350.             A[off + i][off + j] = B[off + i][off + j];
  351. }
  352.  
  353.  
  354. nt
  355. ndex(x, y, w, pad)
  356.     register int x, y, w, pad;
  357. {
  358.     return (x + y * (w + pad));
  359. }
  360.  
  361. rror_diffusion(width, height, out, edge)
  362.     register int width, height;
  363.     register FILE *out;
  364.     register int edge;
  365. {
  366.     register int err, mono_pad;
  367.     register u_short dtmp;
  368.     register u_long i, j;
  369.  
  370.     mono_pad = mpr_linebytes(width, 1) * 8 - width;
  371.  
  372.     if (edge)
  373.         edge_enhancement(width, height, 1);
  374.  
  375.     for (j = 1; j < height + 1; j++) {
  376.         for (i = 1; i < width + 1; i++) {
  377.             dtmp <<= 1;
  378.             if (A[i][j] > 128)
  379.                 err = A[i][j] - 256;
  380.             else {
  381.                 err = A[i][j];
  382.                 dtmp |= 1;
  383.             }
  384.             A[i + 1][j] += (err * ALPHA) >> 4;
  385.             A[i - 1][j + 1] += (err * BETA) >> 4;
  386.             A[i][j + 1] += (err * GAMMA) >> 4;
  387.             A[i + 1][j + 1] += (err * DELTA) >> 4;
  388.             if (((i - 1) % 16) == 15) {
  389.                 putc(dtmp >> 8, out);
  390.                 putc(dtmp, out);
  391.             }
  392.         }
  393.         for (i = width + 1; i <= width + mono_pad; i++) {
  394.             dtmp <<= 1;
  395.             if (((i - 1) % 16) == 15) {
  396.                 putc(dtmp >> 8, out);
  397.                 putc(dtmp, out);
  398.             }
  399.         }
  400.     }
  401. }
  402.  
  403. /* Compute pixel values using error diffusion */
  404. rror_diffusion_image(width, height, map, in, out, edge_enhance, pad)
  405.     register int width, height;
  406.     register unsigned char *map;
  407.     register u_char *in;
  408.     register FILE *out;
  409.     register int edge_enhance, pad;
  410. {
  411.     register int i, j;
  412.  
  413.     for (j = 1; j < height + 1; j++)
  414.         for (i = 1; i < width + 1; i++)
  415.             A[i][j] = map[*(in + index(i - 1, j - 1, width, pad))];
  416.  
  417.     error_diffusion(width, height, out, edge_enhance);
  418. }
  419.  
  420.  
  421. /* Compute pixel values using error diffusion */
  422. rror_diffusion_file(width, height, map, in, out, edge_enhance, pad)
  423.     register int width, height;
  424.     register unsigned char *map;
  425.     register FILE *in, *out;
  426.     register int edge_enhance, pad;
  427. {
  428.     register int i, j, c;
  429.  
  430.     for (j = 1; j < height + 1; j++) {
  431.         for (i = 1; i < width + 1; i++) {
  432.             if ((c = getc(in)) == EOF) {
  433.                 fprintf(stderr, "error reading raster data!\n");
  434.                 exit(1);
  435.             }
  436.             A[i][j] = map[c];
  437.         }
  438.         for (i = width; i < width + pad; i++)
  439.             if ((c = getc(in)) == EOF)
  440.                 fprintf(stderr, "error reading raster data!\n");
  441.     }
  442.  
  443.     error_diffusion(width, height, out, edge_enhance);
  444. }
  445.  
  446. rdered(width, height, out, edge)
  447.     register int width, height;
  448.     register FILE *out;
  449.     register int edge;
  450. {
  451.     register int mono_pad;
  452.     register u_short dtmp;
  453.     register u_long i, j;
  454.  
  455.     mono_pad = mpr_linebytes(width, 1) * 8 - width;
  456.  
  457.     if (edge)
  458.         edge_enhancement(width, height, 0);
  459.  
  460.     for (j = 0; j < 8; j++)
  461.         for (i = 0; i < 8; i++)
  462.             ordered_dither[i][j] = ordered_dither[i][j] * 4 + 2;
  463.  
  464.     for (j = 0; j < height; j++) {
  465.         for (i = 0; i < width; i++) {
  466.             dtmp <<= 1;
  467.             if (A[i][j] >= ordered_dither[i % 8][j % 8])
  468.                 dtmp |= 1;
  469.             if ((i % 16) == 15) {
  470.                 putc(dtmp >> 8, out);
  471.                 putc(dtmp, out);
  472.             }
  473.         }
  474.         for (i = width; i < width + mono_pad; i++) {
  475.             dtmp <<= 1;
  476.             if ((i % 16) == 15) {
  477.                 putc(dtmp >> 8, out);
  478.                 putc(dtmp, out);
  479.             }
  480.         }
  481.     }
  482. }
  483.  
  484. rdered_dither_image(width, height, map, in, out, edge_enhance, pad)
  485.     register int width, height;
  486.     register unsigned char *map;
  487.     register u_char *in;
  488.     register FILE *out;
  489.     register int edge_enhance, pad;
  490. {
  491.     register int i, j;
  492.  
  493.     for (j = 0; j < height; j++)
  494.         for (i = 0; i < width; i++)
  495.             A[i][j] = 256 - map[*(in + index(i, j, width, pad))];
  496.  
  497.     ordered(width, height, out, edge_enhance);
  498. }
  499.  
  500.  
  501. rdered_dither_file(width, height, map, in, out, edge_enhance, pad)
  502.     register int width, height;
  503.     register unsigned char *map;
  504.     register FILE *in, *out;
  505.     register int edge_enhance, pad;
  506. {
  507.     register int i, j, c;
  508.  
  509.     for (j = 0; j < height; j++) {
  510.         for (i = 0; i < width; i++) {
  511.             if ((c = getc(in)) == EOF) {
  512.                 fprintf(stderr, "error reading raster data!\n");
  513.                 exit(1);
  514.             }
  515.             A[i][j] = 256 - map[c];
  516.         }
  517.         for (i = width; i < width + pad; i++)
  518.             if ((c = getc(in)) == EOF)
  519.                 fprintf(stderr, "error reading raster data!\n");
  520.     }
  521.  
  522.     ordered(width, height, out, edge_enhance);
  523. }
  524.  
  525. nt
  526. eight(x, y)
  527.     register int x, y;
  528. {
  529.     return (3 - x * x - y * y);
  530. }
  531.  
  532. ot(width, height, out, edge)
  533.     register int width, height;
  534.     register FILE *out;
  535.     register int edge;
  536. {
  537.     register int k, u, v, err, w, mono_pad;
  538.     register u_short dtmp;
  539.     register u_long i, j;
  540.  
  541.     mono_pad = mpr_linebytes(width, 1) * 8 - width;
  542.  
  543.     if (edge)
  544.         edge_enhancement(width, height, 0);
  545.  
  546.     for (k = 0; k < 64; k++) {
  547.         for (i = reverse_matrix[k] / 8; i < width; i += 8)
  548.             for (j = reverse_matrix[k] % 8; j < height; j += 8) {
  549.                 if (A[i][j] > 128)
  550.                     B[i][j] = 0;
  551.                 else
  552.                     B[i][j] = 1;
  553.                 err = A[i][j] - (1 - B[i][j]) * 256;
  554.                 w = 0;
  555.                 for (u = i - 1; u <= i + 1; u++)
  556.                     for (v = j - 1; v <= j + 1; v++)
  557.                         if ((u >= 0) && (v >= 0) && (dot_diffusion[u % 8][v % 8] > k))
  558.                             w += weight(u - i, v - j);
  559.                 if (w > 0) {
  560.                     for (u = i - 1; u <= i + 1; u++)
  561.                         for (v = j - 1; v <= j + 1; v++)
  562.                             if ((u >= 0) && (v >= 0) && (dot_diffusion[u % 8][v % 8] > k))
  563.                                 A[u][v] += err * weight(u - i, v - j) / w;
  564.                 }
  565.             }
  566.     }
  567.  
  568.     for (j = 0; j < height; j++) {
  569.         for (i = 0; i < width; i++) {
  570.             dtmp <<= 1;
  571.             if (B[i][j])
  572.                 dtmp |= 1;
  573.             if ((i % 16) == 15) {
  574.                 putc(dtmp >> 8, out);
  575.                 putc(dtmp, out);
  576.             }
  577.         }
  578.         for (i = width; i < width + mono_pad; i++) {
  579.             dtmp <<= 1;
  580.             if ((i % 16) == 15) {
  581.                 putc(dtmp >> 8, out);
  582.                 putc(dtmp, out);
  583.             }
  584.         }
  585.     }
  586. }
  587.  
  588.  
  589. ot_diffusion_image(width, height, map, in, out, edge_enhance, pad)
  590.     register int width, height;
  591.     register unsigned char *map;
  592.     register u_char *in;
  593.     register FILE *out;
  594.     register int edge_enhance, pad;
  595. {
  596.     register int i, j;
  597.  
  598.     for (j = 0; j < height; j++)
  599.         for (i = 0; i < width; i++)
  600.             A[i][j] = map[*(in + index(i, j, width, pad))];
  601.  
  602.     dot(width, height, out, edge_enhance);
  603. }
  604.  
  605. ot_diffusion_file(width, height, map, in, out, edge_enhance, pad)
  606.     register int width, height;
  607.     register unsigned char *map;
  608.     register FILE *in, *out;
  609.     register int edge_enhance, pad;
  610. {
  611.     register int i, j, c;
  612.  
  613.     for (j = 0; j < height; j++) {
  614.         for (i = 0; i < width; i++) {
  615.             if ((c = getc(in)) == EOF) {
  616.                 fprintf(stderr, "error reading raster data!\n");
  617.                 exit(1);
  618.             }
  619.             A[i][j] = map[c];
  620.         }
  621.         for (i = width; i < width + pad; i++)
  622.             if ((c = getc(in)) == EOF)
  623.                 fprintf(stderr, "error reading raster data!\n");
  624.     }
  625.  
  626.     dot(width, height, out, edge_enhance);
  627. }
  628.